﻿using CloudModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CommonLibrary
{
    public class Common
    {
        /// <summary>
        /// 验证是否为数字
        /// </summary>
        /// <param name="_string">字符串</param>
        /// <returns></returns>
        public static bool isNumberic(string _string)
        {
            if (!string.IsNullOrWhiteSpace(_string))
            {
                return Regex.IsMatch(_string, @"^[+-]?\d*[.]?\d*$");
            }
            return false;
        }

        /// <summary>
        /// 验证是否为数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsInt(string value)
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                return Regex.IsMatch(value, @"^[+-]?\d*$");
            }
            return false;
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        public static string GetToken(string serverIP, string userName, string pwd, string projectTag)
        {
            string token = "";
            HttpHelper helper = new HttpHelper(serverIP);
            Users users = new Users();
            users.UserName = userName;
            users.Password = pwd;
            users.ProjectTag = projectTag;
            helper.RequestData(users, "Account", "Logon");
            string strResult = helper.ResultMsg;


            if (!string.IsNullOrWhiteSpace(strResult))
            {
                ResponseModel response = JsonConvert.DeserializeObject<ResponseModel>(strResult);

                if (response != null)
                {
                    if (response.Status == ResultStatus.Success)
                    {
                        if (response.AppendData != null && !string.IsNullOrWhiteSpace(response.AppendData.ToString()))
                        {
                            AccessTokenModel atm = JsonConvert.DeserializeObject<AccessTokenModel>(response.AppendData.ToString());
                            token = atm.AccessToken;
                        }
                    }
                }
            }
            return token;
        }
    }
}
